require 'rubygems'
require 'RMagick'
  
class PicturesController < ApplicationController
  include AuthenticatedSystem
  #before_filter :login_required, only => [:index, :new, :edit, :create, :udpate, :destroy]
  before_filter   :login_required, :except => [:show] 
  #before_filter :admin_required, :only => [:edit]
  
  # GET /pictures
  # GET /pictures.xml
  def index
    
    # Si l'acteur connecté est un administrateur
    if isAdmin?()
       @pictures = Picture.find(:all)
       @isAdmin = true
    # Si l'acteur connecté est un membre
    else
       @pictures = Picture.find(:all, :conditions => ["user_id = :id",{:id => current_user.id}])
       @isAdmin = false
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @pictures }
    end
  end

  # GET /pictures/1
  # GET /pictures/1.xml
  def show
    @picture = Picture.find(params[:id])

    # Si l'acteur connecté est un administrateur
    if isAdmin?()
       @isAdmin = true
    # Si l'acteur connecté est un membre
    else
       @isAdmin = false
    end
    
    @comments = @picture.comment

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @picture }
    end
  end

  # GET /pictures/new
  # GET /pictures/new.xml
  def new
    @picture = Picture.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @picture }
    end
  end

  # GET /pictures/1/edit
  def edit   
    @picture = Picture.find(params[:id])
    
    # Image modifier uniquement par le propriétaire
    if (@picture.user_id != current_user.id && !isAdmin?())
      redirect_to(@picture)
    end
  end

  # POST /pictures
  # POST /pictures.xml
  def create
    @picture = Picture.new
    @picture.title = params[:pictures]['name']
    @picture.description = params[:pictures]['description']
    @picture.user_id = current_user.id
    @picture.extension = ""
    
    # Recherche du dernier id
    if Picture.find(:last) == nil
      @picture.id = 0
    else
      @picture.id = Picture.find(:last).id + 1
    end    
    
    # Définition de l'extension en fonction du type de l'image
    if params[:upload]['datafile'] != ""
      if params[:upload]['datafile'].content_type == "image/jpeg"
        @picture.extension = "jpg"
      elsif params[:upload]['datafile'].content_type == "image/png"
        @picture.extension = "png"
      elsif params[:upload]['datafile'].content_type == "image/gif"
        @picture.extension = "gif"
      end
    end

    respond_to do |format|
      if @picture.extension!="" && @picture.save
        # Enregistre la photo sur le serveur
        post = DataFile.save(params[:upload], @picture.extension, @picture.id)
        
        flash[:notice] = 'Picture was successfully created.'
        format.html { redirect_to(@picture) }
        format.xml  { render :xml => @picture, :status => :created, :location => @picture }
      else
        flash[:error] = 'L\'image à envoyer doit porter une des extensions suivantes: JPG/PNG/GIF'
        
        format.html { render :action => "new" }
        format.xml  { render :xml => @picture.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /pictures/1
  # PUT /pictures/1.xml
  def update
    @picture = Picture.find(params[:id])

    respond_to do |format|
      if @picture.update_attributes(params[:picture])
        flash[:notice] = 'Picture was successfully updated.'
        format.html { redirect_to(@picture) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @picture.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /pictures/1
  # DELETE /pictures/1.xml
  def destroy
    @picture = Picture.find(params[:id])
    
    link = "public/images/pictures/#{@picture.id}.#{@picture.extension}"
    linkSmall = "public/images/pictures/#{@picture.id}-small.#{@picture.extension}"
    
    # Supprimer la photo ainsi que sa miniature
    if File.exist?(link)
      File.delete(link) 
    end
    if File.exist?(linkSmall)
      File.delete(linkSmall) 
    end
    
    @picture.destroy

    respond_to do |format|
      format.html { redirect_to(pictures_url) }
      format.xml  { head :ok }
    end
  end
end
